package com.lagou.service.impl;

import com.lagou.dao.RoleMapper;
import com.lagou.domain.*;
import com.lagou.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 李树飞
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> findAllRole(Role role) {
        return roleMapper.findAllRole(role);
    }

    @Override
    public List<Integer> findMenuByRoleId(Integer roleId) {
        return roleMapper.findMenuByRoleId(roleId);
    }

    @Override
    public void roleContextMenu(RoleMenuVo roleMenuVo) {

        //清空中间表
        roleMapper.deleteRoleContextMenu(roleMenuVo.getRoleId());

        //为角色分配菜单信息
        for (Integer mid : roleMenuVo.getMenuIdList()) {
            Role_menu_relation role_menu_relation = new Role_menu_relation();
            role_menu_relation.setMenuId(mid);
            role_menu_relation.setRoleId(roleMenuVo.getRoleId());

            role_menu_relation.setCreatedTime(new Date());
            role_menu_relation.setUpdatedTime(new Date());
            role_menu_relation.setCreatedBy("system");
            role_menu_relation.setUpdatedby("system");

            roleMapper.roleContextMenu(role_menu_relation);
        }

    }

    @Override
    public void deleteRole(Integer roleId) {
        roleMapper.deleteRoleContextMenu(roleId);
        roleMapper.deleteRole(roleId);
    }

    @Override
    public List<ResourceCategory> findResourceListByRoleId(Integer roleId) {
        //查询当前角色拥有的资源信息
        List<Resource> resourceList = roleMapper.findResourceByRoleId(roleId);

        if (resourceList.isEmpty()) {
            return null;
        }
        //查询当前角色拥有的资源分类信息
        List<ResourceCategory> resourceCategoryList = roleMapper.findResourceCategoryByRoleId(roleId);

        //将资源信息封装到对应的分类下
        for (Resource resource : resourceList) {
            for (ResourceCategory resourceCategory : resourceCategoryList) {
                if (resource.getCategoryId().equals(resourceCategory.getId())) {
                    resourceCategory.getResourceList().add(resource);
                    break;
                }
            }
        }
        return resourceCategoryList;
    }

    @Override
    public void roleContextResource(RoleResourceVo roleResourceVo) {
        //删除角色关联的资源信息
        roleMapper.deleteRoleContextResource(roleResourceVo.getRoleId());

        //遍历资源id，封装数据
        for (Integer resourceId : roleResourceVo.getResourceIdList()) {
            RoleResourceRelation roleResourceRelation = new RoleResourceRelation();
            roleResourceRelation.setResourceId(resourceId);
            roleResourceRelation.setRoleId(roleResourceVo.getRoleId());

            Date date = new Date();
            roleResourceRelation.setCreateTime(date);
            roleResourceRelation.setUpdateTime(date);
            roleResourceRelation.setCreatedBy("system");
            roleResourceRelation.setUpdatedBy("system");

            roleMapper.roleContextResource(roleResourceRelation);
        }
    }
}
